package cn.sumpu.app.coolsk.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.coolsk.dao.HotelRoomDao;
import cn.sumpu.app.coolsk.domain.Hotel;
import cn.sumpu.app.coolsk.domain.Room;
import cn.sumpu.app.coolsk.domain.RoomPrice;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class HotelRoomDaoImpl extends SqlSessionDaoSupport implements HotelRoomDao{

	private static Logger logger = LoggerFactory.getLogger(HotelRoomDaoImpl.class);
	@Override
	public void insertHotel(Hotel hotel) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.insertHotel",
							hotel);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@Override
	public void updateHotel(Hotel hotel) throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.updateHotel",
					hotel);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Hotel> getHotel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.getHotel",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public void delHotel(Map<String,Object> map) throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.delHotel",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@Override
	public void insertRoom(Room room) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.insertRoom",
							room);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@Override
	public void updateRoom(Room room) throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.updateRoom",
					room);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Room> getRoom(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.getRoom",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public void delRoom(Map<String, Object> map) throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.delRoom",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@Override
	public int getHotelCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.getHotelCount",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@Override
	public void insertRoomPrice(RoomPrice roomPrice) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.insertRoomPrice",
							roomPrice);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@Override
	public void delRoomPrice(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.delRoomPrice",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RoomPrice> getRoomPrice(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.getSpecialRoomPrice",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public int getRoomCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.getRoomCount",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Hotel> searchHotel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.searchHotel",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("searchHotel failure.", e);
			}
			throw new AppBusinessException("searchHotel failure.", e);
		}
	}

	@Override
	public int searchHotelCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.HotelRoomMapper.searchHotelCount",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("searchHotelCount failure.", e);
			}
			throw new AppBusinessException("searchHotelCount failure.", e);
		}
	}

}
